<?php
/*
Plugin Name: Google Product Feed
Plugin URI: http://wp-jacob.googlecode.com
Description: Extends the Shopping Cart plugin to provide a product feed for Google
Version: 0.2
Author: Jacob Brunson
Author URI: http://jacobbrunson.com
*/

/*  Copyright 2008  Jacob Brunson  (email : wp-jacob.gpl.license@jacobbrunson.com )

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


function gpfeed_allow_only_products_in_feed($query)
{
    if (!isset($_GET['gpfeed'])) return $query;
    if ($query->is_feed) {
        $query->set('meta_key', 'gpfeed_enabled');
        $query->set('meta_value', 'true');
    }  
    return $query;
}
add_filter('pre_get_posts','gpfeed_allow_only_products_in_feed');


function shopcart_add_feed_entry_meta_data()
{
    global $post;
    if (!isset($_GET['gpfeed'])) return;
    
    echo "\n<title>{$post->post_title}</title>\n";
    
    echo "<summary>";
    the_excerpt_rss();
    echo "</summary>\n";
    
    $price = get_post_meta($post->ID, 'shopcart_price', true);
    echo "<g:price>$price</g:price>\n";
    
    $image = get_post_meta($post->ID, 'shopcart_image', true);
    if ($image != null && trim($image) != '') {
        echo "<g:image_link>$image</g:image_link>\n";
    }
    
    $upc = get_post_meta($post->ID, 'shopcart_upc', true);
    if ($upc != null && trim($upc) != '') {
        echo "<g:upc>$upc</g:upc>\n";
    }
    
    $brand = get_post_meta($post->ID, 'shopcart_brand', true);
    if ($brand != null && trim($brand) != '') {
        echo "<g:brand>$brand</g:brand>\n";
    }
    
    $mpn = get_post_meta($post->ID, 'shopcart_mpn', true);
    if ($mpn != null && trim($mpn) != '') {
        echo "<g:mpn>$mpn</g:mpn>\n";
    }
    
    $weight_meta = get_post_meta($post->ID, 'shopcart_weight', true);
    $weight_units = get_option('shopcart_weight_units');
    if ($weight_meta != null && trim($weight_meta) != '' && (int)$weight_meta > 0) {
        echo "<g:weight>$weight_meta $weight_units</g:weight>\n";
    }
    
    $condition_meta = get_post_meta($post->ID, 'shopcart_condition', true);
    echo "<g:condition>$condition_meta</g:condition>\n";
}
add_action('google_base_atom_entry', 'gpfeed_add_feed_entry_meta_data');


function gpfeed_add_shopcart_meta_fields()
{
    shopcart_add_item_meta_field('Brand', 'shopcart_brand', 'input');
}
add_action('shopcart_plugin_loaded', 'gpfeed_add_shopcart_meta_fields');


function gpfeed_generate($comments = "")
{
    header('Content-Type: ' . feed_content_type('atom') . '; charset=' . get_option('blog_charset'), true);
    $more = 1;
    echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
    ?>
        <feed
        xmlns="http://www.w3.org/2005/Atom"
        xmlns:thr="http://purl.org/syndication/thread/1.0"
        xml:lang="<?php echo get_option('rss_language'); ?>"
        xml:base="<?php bloginfo_rss('home') ?>/wp-atom.php"
        xmlns:g="http://base.google.com/ns/1.0"
        <?php do_action('atom_ns'); ?>
        >
            <title type="text"><?php bloginfo_rss('name'); wp_title_rss(); ?></title>
            <subtitle type="text"><?php bloginfo_rss("description") ?></subtitle>

            <updated><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_lastpostmodified('GMT'), false); ?></updated>
            <?php the_generator( 'atom' ); ?>

            <link rel="alternate" type="text/html" href="<?php bloginfo_rss('home') ?>" />
            <id><?php bloginfo('atom_url'); ?></id>
            <link rel="self" type="application/atom+xml" href="<?php self_link(); ?>" />

            <?php do_action('google_base_atom_head'); ?>
            <?php while (have_posts()) : the_post(); ?>
            <entry>
                <title><![CDATA[<?php the_title_rss() ?>]]></title>
                <link href="<?php the_permalink_rss() ?>" />
                <id><?php the_guid(); ?></id>
                <updated><?php echo get_post_modified_time('Y-m-d\TH:i:s\Z', true); ?></updated>
                <published><?php echo get_post_time('Y-m-d\TH:i:s\Z', true); ?></published>
                <summary><![CDATA[<?php the_excerpt_rss(); ?>]]></summary>
                
                <?php do_action('google_base_atom_entry'); ?>
            </entry>
            <?php endwhile ; ?>
        </feed>
    <?php
}

function gpfeed_init()
{
    add_feed('products','gpfeed_generate');
}
add_action('init','gpfeed_init');